package com.toidep.utilities;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.toidep.R;
import com.toidep.activities.MainActivity;
import com.toidep.api.ApiController;
import com.toidep.api.ParseJSON;
import com.toidep.config.Constants;
import com.toidep.entities.DetailEmailEntity;
import com.toidep.entities.EmailEntity;
import com.toidep.interfaces.ConfirmChangePassListener;
import com.toidep.interfaces.ConfirmListener;
import com.toidep.interfaces.DialogListener;
import com.toidep.interfaces.ResponseListener;
import com.toidep.preference.SharePreference;

public class DialogUtility {
	
	public static void showDialogAlert(final Activity mbBaseActivity, String title, String message, String buttonLabel, final DialogListener dialogListener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(mbBaseActivity);
		if (title == null || title == "")
			builder.setTitle(R.string.app_name);
		else 
			builder.setTitle(title);
		builder.setMessage(message);
		builder.setCancelable(true);
		builder.setIcon(R.drawable.ic_launcher);
		builder.setPositiveButton(buttonLabel, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				if (dialogListener != null)
					dialogListener.after();
			}
		});
		if (mbBaseActivity != null && !mbBaseActivity.isFinishing()) {
			builder.show();
		}
	}

	public static void showDialogConfirm(final Activity activity, String title, String message, final ConfirmListener confirmListener) {

		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		if (title == null || title == "")
			builder.setTitle(R.string.app_name);
		else 
			builder.setTitle(title);
		builder.setIcon(R.drawable.ic_launcher);
		builder.setMessage(message);
		builder.setCancelable(false);
		builder.setNegativeButton(R.string.label_no, new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				confirmListener.doCancel();
			}
		});

		builder.setPositiveButton(R.string.label_yes, new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				confirmListener.doAccept();
			}
		});
		AlertDialog alert = builder.create();
		alert.setCancelable(true);
		if (activity != null && !activity.isFinishing()) {
			alert.show();	
		}
	}
	
	public static void showDialogChangePassword(final Activity activity, String title, String message,
			final ConfirmChangePassListener confirmListener) {
		if (activity.isFinishing())
			return;
		
		final Dialog dialog = new Dialog(activity, R.style.PopupThemeDialog);
		
		dialog.setContentView(R.layout.layout_edit_password);
		Button btnUpdatePass = (Button) dialog.findViewById(R.id.buttonOKChangePassword);
		btnUpdatePass.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				final String oldPass = ((TextView) dialog.findViewById(R.id.edittextPasswordOld)).getEditableText().toString();
				final String newPass = ((TextView) dialog.findViewById(R.id.edittextPasswordNew)).getEditableText().toString();
				final String newPassAgain = ((TextView) dialog.findViewById(R.id.edittextPasswordNewAgain)).getEditableText().toString();
				
				// Validate mat khau moi
				if (oldPass == null || oldPass.length() == 0 || 
						newPass == null || newPass.length() == 0 || 
						newPassAgain == null || newPassAgain.length() == 0) {
					DialogUtility.showDialogAlert(
							activity, 
							"", 
							activity.getString(R.string.text_null_field), 
							activity.getString(R.string.label_close), 
							new DialogListener() {
								
								@Override
								public void after() {
									// TODO Auto-generated method stub
								}
							});
					return;
				}
				if (!newPass.equals(newPassAgain)) {
					DialogUtility.showDialogAlert(
							activity, 
							"", 
							activity.getString(R.string.text_password_not_match), 
							activity.getString(R.string.label_close),  
							new DialogListener() {
								
								@Override
								public void after() {
									// TODO Auto-generated method stub
								}
							});
					return;
				}
				
				// Gui request Thay doi mat khau len Server
				SharePreference sharePreference = SharePreference.getInstance(activity);
				String currentAccessToken = sharePreference.getAccessToken();
				
				try {
					ApiController.changePassword(
							activity, 
							oldPass, 
							newPassAgain, 
							currentAccessToken, 
							new ResponseListener() {
								
								@Override
								public void processResponse(int inintstall) {
									showAlertError();
								}
								
								@Override
								public void processResponse(String response) {
									Log.e("editPassword response", response);
									
									try {
										if (response != null && response != "" ) {
											JSONObject jsonObject = new JSONObject(response);
											if (jsonObject != null) {
												int code = ParseJSON.getIntValue(jsonObject, Constants.PARAM_RESPONSE_CODE);
												 if (code > 0) {
													DialogUtility.showDialogAlert(
															activity, 
															"", 
															activity.getString(R.string.text_change_password_success), 
															activity.getString(R.string.label_close),  
															new DialogListener() {
																
																@Override
																public void after() {
																	confirmListener.doAccept(oldPass, newPass, newPassAgain);
																	dialog.dismiss();
																}
															});
												} else {
													DialogUtility.showDialogAlert(
															activity, 
															"", 
															ParseJSON.getStringValue(jsonObject, Constants.PARAM_RESPONSE_MESSAGE), 
															activity.getString(R.string.label_close),  
															new DialogListener() {
																
																@Override
																public void after() {
																	
																}
															});
												}
											} else {
												showAlertError();
											}
										} else {
											showAlertError();
										}
									} catch (JSONException e) {
										e.printStackTrace();
										showAlertError();
									}
								}
								
								private void showAlertError() {
									DialogUtility.showDialogAlert(
											activity, 
											"", 
											activity.getString(R.string.text_change_password_error), 
											activity.getString(R.string.label_close),  
											new DialogListener() {
												
												@Override
												public void after() {
												}
											});
								}
							});
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		});
		Button btnCancel = (Button) dialog.findViewById(R.id.buttonCancelChangePassword);
		btnCancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				confirmListener.doCancel();
				dialog.dismiss();
			}
		});
		
		dialog.setCancelable(false);
		dialog.show();
	}

	public static void showDialogEmailDetail(FragmentActivity activity,
			EmailEntity currentItem) {
		if (activity.isFinishing())
			return;
		
		final Dialog dialog = new Dialog(activity, R.style.PopupTheme);
		
		dialog.setContentView(R.layout.layout_email_detail);
		
		TextView textTitle = (TextView) dialog.findViewById(R.id.textTitle);
		final TextView textDetail = (TextView) dialog.findViewById(R.id.textDetail);
		
		textTitle.setText(currentItem.getSubject());
//		textDetail.setText(currentItem.getSubject());
		
		dialog.setCancelable(true);
		dialog.show();
		ResponseListener getDetailResponse = new ResponseListener() {
			
			@Override
			public void processResponse(int inintstall) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void processResponse(String response) {
				// TODO Auto-generated method stub
				dialog.findViewById(R.id.loadingBar).setVisibility(View.GONE);
				DetailEmailEntity detail = ParseJSON.getDetailEmailsFromResponse(response);
				if (detail != null) {
					textDetail.setText(detail.getBody());
				} else {
					textDetail.setText("");
				}
				
			}
		};
		
		ApiController.getDetailEmails(activity, currentItem.getId(), getDetailResponse);
	}
	
}
